/* Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "ufo.h"

#include "gameengine.h"

#include <QDebug>
#include <math.h>

/*! \class Ufo
  Game object for ufo enemy. Ufo rotates on screen.
*/

/*!
  Constructor for Ufo.

  \a frame Current frame number. When the GameObject was created.

  \a rewindEnabled Is the rewind mode enabled.

  \a movement

  \a direction
*/
Ufo::Ufo(int frame, bool rewindEnabled, UfoMovement movement, FPtype direction) 
    : GameObject(TypeUfo, frame, GameEngine::GameObjectRadiusUFO, SpeedLimitLow, rewindEnabled, 
               MassMedium, ScoreUfo),
      movement(movement), direction(direction), movementPhase(90.0f * M_PI / 180.0f)
{
}

/*!
 * Calculate steering and a new velocity for UFO based on 
 * selected movement mode.
 *
 * First a \c Ufo::movementPhase value is calculated. This is used for calculating
 * the UFO phase when \c SineMovement or \c CircularMovement is selected.
 *
 * To change UFO movement, add new value to Ufo::UfoMovement and new
 * logic here accordingly, e.g.
 * \code
 * switch (movement) {
 * case SineMovement:
 *      tmpX = 5.0f;
 *      tmpY = 15.0f * sin(movementPhase);
 *      break;
 *      ...
 * }
 * \endcode
 *
 *
 * \param timestep A timestep calculated for current game cycle (current game frame length) in milliseconds.
 */
void Ufo::doUfoSteering(FPtype timestep)
{
    movementPhase += 0.0025 * timestep;
    if (movementPhase > 2 * M_PI)    // wrap at 2pi.
        movementPhase -= 2 * M_PI;

    FPtype tmpX = 0.0f;
    FPtype tmpY = 0.0f;

    FPtype scale;

    // here we write the UFO velocities based on the
    // selected movement mode.

    // notice that the initial value of movementPhase
    // is 90 degrees which helps to "center" the UFO
    // movement to the planned trajectory.

    switch (movement) {
    case SineMovement:
        tmpX = 5.0f;
        tmpY = 15.0f * sin(movementPhase);
        break;
    case CircularMovement:
        scale = 1.0f + cos(movementPhase) * 0.5f;
        tmpX = 10.0f * cos(movementPhase) * scale;
        tmpY = 10.0f * sin(movementPhase);
        break;
    default:
        qCritical() << "Ufo::doUfoSteering() : unknown movement = " + movement;
        qFatal("Ufo::doUfoSteering() : bad movement!");
    }

    FPtype velX = 0.0f;
    FPtype velY = 0.0f;

    velX += sin(direction) * tmpX;
    velY += cos(direction) * tmpX;

    velX -= cos(direction) * tmpY;  // a negative term!
    velY += sin(direction) * tmpY;

    setVelX(velX);
    setVelY(velY);
}
